#include "Engine.h"
#include <iostream>
#include <time.h>

#include <GL\glut.h>
#include "Vector3.h"
#include "Window.h"
#include "glutKeyboardManager.h"
#include "glutMouseManager.h"

#include "MyMatrix.h"
#include "World.h"

/* this is hear to remove the game from the glut stuff
its real use is to manage most of the glut functionality in one place
other than the actual drawing witch takes place in the game
*/

//using namespace std;

BaseGame* Engine::game1 = new World();

int Engine::oldTime = 0;
int Engine::currentTime = 0;
float Engine::dt = 0;

Engine::Engine()
{
	//game1 = new Game();
	srand(time(NULL));
}

void display(void)
{
	Engine::oldTime = Engine::currentTime;
	Engine::currentTime = glutGet(GLUT_ELAPSED_TIME);
	Engine::dt = (Engine::currentTime - Engine::oldTime)/1000.0f; 
	glutSetWindow(window);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

	//glTranslatef(); camra postion
	//glMultMatrixf();

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	//////////////////////
	Engine::game1->Update();
	Engine::game1->Draw();
		//update input after current frame for them to work
	glutKeyboardManager::update();
	glutMouseManager::Update();
	//////////////////////
	glutSwapBuffers();
	glutPostRedisplay();
}


void initWindow()
{
	glEnable(GL_CULL_FACE);
	glCullFace(GL_FRONT);
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);
	glClearColor (0.0, 0.0, 0.0, 1.0);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1.0, 0.0, 0.0);
      
	// FLIP THE Y TO HAVE IT WORK LIKE ACTIONSCRIPT CORDINATES 
	//glOrtho(0, VIEW_WIDTH, VIEW_HEIGHT, 0, 0, 500); //AS3

	// normal
	//glOrtho(0, VIEW_WIDTH, 0, VIEW_HEIGHT, -500, 500);	// C++

	//gluPerspective(1.4,1,0.1,1000);
	Engine::game1->init();
}

int main(int argc, char **argv)
{
	
	//Engine eg();
	//AutoPolygon poly(pos,5, 50);

	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize(VIEW_WIDTH, VIEW_HEIGHT);
	// depth stuff
	//glClearDepth(1.0f);
	
	///
	window = glutCreateWindow("Cube View");
	glutDisplayFunc(display);
	//glutReshapeFunc(reshape);

	initWindow();
	
	glutKeyboardManager::begin();
	glutMouseManager::Begin();

	Engine::oldTime = glutGet(GLUT_ELAPSED_TIME);
	Engine::currentTime = glutGet(GLUT_ELAPSED_TIME);
	
	glutMainLoop();
	return 0;
}





